%   multiloss_main v.3 (02/28/2011)
%   This program replicates the empirical results from Komunjer and Owyang
%   "Multivariate Forecast Evaluation and Rationality Testing"
%   Real time data used as instruments is available at http://econ.ucsd.edu/~ikomunje/contact.htm
%   Forecast data is proprietary.  Contact http://www.aspenpublishers.com/blue-chip-publications.htm
%   ****************
%   The program requires forecast errors and a set of instruments

%   Required files/toolboxes
%       Optimization Toolbox
%   These estimate the loss functions and the compute the J stats
%       gmm_multi_ik
%       GMMmultiloss3
%   These compute the objective function for the multivariate loss
%       ObjFctGMMmulti2
%       GamaM_2
%       WeightWT_2
%   These compute the objective function for the separable loss
%       ObjFctGMMuni1
%       GamaM_1
%       WeightWT_1
%       PPgrad



clear
global P ERROR_SERIES INSTRUMENTS;
%   Load the forecast errors and the instruments here:
load('C:\Users\h1mto00\Desktop\Ivana Code ReSTAT\fDATA3')
%   *************************************************************
%   NOTE: THE FORECAST ERROR DATA IS PROPRIETARY AND IS NOT INCLUDED
%   *************************************************************
%   f will collect the number of valid observations for each forecaster
f = [];
%   Iterate over all forecasters
for n = 1:size(fDATA,2)
    ERROR_SERIES = fDATA(n).e;
    INST = fDATA(n).X;
    %   Check to see what data is available for the nth forecaster
    ind1 = logical(1-isnan(ERROR_SERIES));
    ind2 = logical(1-isnan(INST));
    indall = [ind1, ind2];
    indOK = all(indall,2);
    %   Compute the number of  valid observations
    flagg = sum(indOK);
    %   If the number of valid observations is too small, skip that
    %   forecaster
    f = [f; flagg];
    %   We set the minimum number of observations at 80
    if flagg >= 80
        %   Choose only valid observations
        ERROR_SERIES = ERROR_SERIES(indOK,:);
        INST = INST(indOK,:);
        %   Expresses forecast errors back in real numbers (as opposed to percentages)
        %   ERROR_SERIES = ERROR_SERIES / 100;
        %   Crates the different instrument sets
        [inst] = shapeinst3(INST);
        %   Loop over the number of instrument sets
        for i = 1:size(inst,2)
            INSTRUMENTS = inst(i).X;
            %   Estimation with P fixed!!
            for p = 1:2;
                disp(['iteration ',num2str(n),' ',num2str(i),' ',num2str(P)])
                %   This calls the estimation
                P = p;
                [xx1] = gmm_multi3ik(ERROR_SERIES,INSTRUMENTS,p);
                results1(n,i,p) = xx1;
                %results2(n,i,p) = xx2;
            end,
        end,
    end
end,